package hero_info

import (
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
)

func (s *Hero_info_service) S2C_EquipCell(conn ziface.IConnection, roleid int64, msg *pb.EquipCellC2S) pb.ErrorCodeManager {
	equipcells := make(map[int32][]int32)
	equipcells[msg.Heroserid] = make([]int32, 2)
	equipcells[msg.Heroserid][0] = msg.Cellserid
	equipcells[msg.Heroserid][1] = msg.Cellidx

	hero := Hero_info_serviceObj.LoadByRoleId(roleid)
	heros, cells, ok, code := s.onEquipCell(hero, equipcells)
	if ok {
		msgS2C := &pb.UpdateHeroCellS2C{
			Heros: heros,
			Cells: cells,
		}

		defer func() {
			if msgS2C != nil {
				zlog.Debugf("ProtocolManager_UpdateHeroCell %v", msgS2C)
				defer func() {
					conn.SendBuffPBMsg(uint32(pb.ProtocolManager_UpdateHeroCell), msgS2C)
				}()
			}
		}()
	}
	return code
}

// equipcells map[int32:英雄Serid][0:细胞Serid,1:装备位置索引]int32
func (s *Hero_info_service) onEquipCell(hero *Cache_hero_info, equipcells map[int32][]int32) ([]*pb.HeroCardData, []*pb.HeroCellData, bool, pb.ErrorCodeManager) {

	rstHeros := util.NewSet[int32]()     //有变更的英雄组
	rstRoleCells := util.NewSet[int32]() //有变更的细胞组

	maxEquipCnt := int(Hero_info_serviceObj.GetHeroEquipCellMaxCount()) //最大装备细胞数量

	if equipcells != nil && hero != nil && hero.Heros != nil && hero.RoleCells != nil {
		for heroserid, cells := range equipcells {
			cellserid := cells[0]
			cellidx := int(cells[1])
			if cellidx < maxEquipCnt { //要装备的位置没有超过最大数
				if hv, h1 := hero.Heros[heroserid]; h1 { //要装备的英雄存在
					if hv.Cells == nil {
						hv.Cells = make(map[int]*Cache_hero_info_field_cell)
					}

					if extrcv, c1 := hero.RoleCells[cellserid]; c1 { //要装备的细胞存在
						if extrcv.Count > 0 { //细胞数量足够
							if oldcell := hv.Cells[cellidx]; oldcell != nil { //当前位置有细胞
								if oldcell.Serid != extrcv.Serid { //要装备的细胞与已装备的细胞不一样
									if orc, o1 := hero.RoleCells[oldcell.Serid]; o1 {
										orc.Count++
									} else {
										hero.RoleCells[oldcell.Serid] = &Cache_hero_info_field_cell{
											Serid: oldcell.Serid,
											CfgId: oldcell.CfgId,
											Lv:    oldcell.Lv,
											Count: 1,
											Idx:   oldcell.Idx,
										}
									}
									delete(hv.Cells, cellidx)
									extrcv.Count--
									rstHeros.Add(hv.SerId)          //英雄数据有变更
									rstRoleCells.Add(oldcell.Serid) //角色细胞数据有变更
									rstRoleCells.Add(extrcv.Serid)  //角色细胞数据有变更
								}
							} else { //当前位置没细胞
								hv.Cells[cellidx] = &Cache_hero_info_field_cell{
									Serid: extrcv.Serid,
									CfgId: extrcv.CfgId,
									Lv:    extrcv.Lv,
									Count: 1,
									Idx:   int32(cellidx),
								}
								extrcv.Count--
								rstHeros.Add(hv.SerId)         //英雄数据有变更
								rstRoleCells.Add(extrcv.Serid) //角色细胞数据有变更
							}

							rstS2CHeros := make([]*pb.HeroCardData, 0)
							rstS2CHeroCells := make([]*pb.HeroCellData, 0)

							if rstHeros != nil {
								for _, hid := range rstHeros.Elements() {
									if thv, th1 := hero.Heros[hid]; th1 {
										rstS2CHeros = append(rstS2CHeros, thv.ToPB_HeroCardData())
									}
								}
							}

							if rstRoleCells != nil {
								for _, cid := range rstRoleCells.Elements() {
									if tcv, tc1 := hero.RoleCells[cid]; tc1 {
										rstS2CHeroCells = append(rstS2CHeroCells, tcv.To_PbData())
									}
								}
							}

							if extrcv.Count <= 0 {
								delete(hero.RoleCells, extrcv.Serid)
							}

							go hero.SaveDB_HeroInfo_Cells()
							return rstS2CHeros, rstS2CHeroCells, true, pb.ErrorCodeManager_SUCCESS
						} else {
							rstS2CHeroCells := make([]*pb.HeroCellData, 1)
							rstS2CHeroCells[0] = extrcv.To_PbData()
							delete(hero.RoleCells, extrcv.Serid)

							go hero.SaveDB_HeroInfo_Cells()
							return nil, rstS2CHeroCells, true, pb.ErrorCodeManager_SUCCESS
						}
					}
				}
			}
		}
	}
	return nil, nil, false, pb.ErrorCodeManager_HERO_INFO_ERROR_EQUIPCELL
}
